import java.util.ArrayList;
import java.util.List;

public class inorderTraversal {
    /**
     * 二叉树的中序遍历,左根右
     */
    public class TreeNode{
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(){};
        TreeNode(int val){
            this.val=val;
        }
        TreeNode(int val,TreeNode left,TreeNode right){
            this.val=val;
            this.left=left;
            this.right=right;
        }
    }

    public List<Integer> method(TreeNode root){
        List<Integer> res=new ArrayList<>();
        dfs(res,root);
        return res;
    }
    void dfs(List<Integer>res,TreeNode root){
        if(root == null){
            return;
        }
        dfs(res,root.left);
        res.add(root.val);
        dfs(res,root.right);
    }

    public static void main(String[] args) {
        inorderTraversal r=new inorderTraversal();
        TreeNode root=r.new TreeNode(1);
        root.right = r.new TreeNode(2);
        root.right.left = r.new TreeNode(3);
        root.right.left.right = r.new TreeNode(4);

        // 进行中序遍历
        List<Integer> result = r.method(root);


        System.out.println(result.toString());
    }
}
